--[[
Title: ItemBlockModel
Author(s): LiXizhi
Date: 2015/5/5
Desc: 
use the lib:
------------------------------------------------------------
NPL.load("(gl)script/apps/Aries/Creator/Game/Items/ItemBlockModel.lua");
local ItemBlockModel = commonlib.gettable("MyCompany.Aries.Game.Items.ItemBlockModel");
local item = ItemBlockModel:new({icon,});
-------------------------------------------------------
]]
NPL.load("(gl)script/apps/Aries/Creator/Game/Common/Files.lua");
local Files = commonlib.gettable("MyCompany.Aries.Game.Common.Files");
local EntityManager = commonlib.gettable("MyCompany.Aries.Game.EntityManager");
local BlockEngine = commonlib.gettable("MyCompany.Aries.Game.BlockEngine")
local block_types = commonlib.gettable("MyCompany.Aries.Game.block_types")
local GameLogic = commonlib.gettable("MyCompany.Aries.Game.GameLogic")
local ItemStack = commonlib.gettable("MyCompany.Aries.Game.Items.ItemStack");

local ItemBlockModel = commonlib.inherit(commonlib.gettable("MyCompany.Aries.Game.Items.Item"), commonlib.gettable("MyCompany.Aries.Game.Items.ItemBlockModel"));

block_types.RegisterItemClass("ItemBlockModel", ItemBlockModel);

local default_inhand_offset = {0.15, 0.3, 0}

-- @param template: icon
-- @param radius: the half radius of the object. 
function ItemBlockModel:ctor()
end

-- item offset when hold in hand. 
-- @return nil or {x,y,z}
function ItemBlockModel:GetItemModelInHandOffset()
	return self.inhandOffset or default_inhand_offset;
end


function ItemBlockModel:TryCreate(itemStack, entityPlayer, x,y,z, side, data, side_region)
	local local_filename = itemStack:GetDataField("tooltip");
	local filename = local_filename;
	if(filename) then
		filename = Files.GetWorldFilePath(filename);
	end
	if(not filename) then
		self:OpenChangeFileDialog(itemStack);
		return;
	end

	if (itemStack and itemStack.count == 0) then
		return;
	elseif (entityPlayer and not entityPlayer:CanPlayerEdit(x,y,z, data, itemStack)) then
		return;
	elseif (self:CanPlaceOnSide(x,y,z,side, data, side_region, entityPlayer, itemStack)) then
		local x_, y_, z_ = BlockEngine:GetBlockIndexBySide(x,y,z,BlockEngine:GetOppositeSide(side));
		local last_block_id = BlockEngine:GetBlockId(x_, y_, z_);
		local block_id = self.block_id;

		local block_template = block_types.get(block_id);
		if(block_template) then
			data = data or block_template:GetMetaDataFromEnv(x, y, z, side, side_region);
			
			local xml_data = {attr = {filename = local_filename} };
			if(BlockEngine:SetBlock(x, y, z, block_id, data, 3, xml_data)) then
				block_template:play_create_sound();

				block_template:OnBlockPlacedBy(x,y,z, entityPlayer);
				if(itemStack) then
					itemStack.count = itemStack.count - 1;
				end
			end
			return true;
		end
	end
end

function ItemBlockModel:PickItemFromPosition(x,y,z)
	local entity = self:GetBlock():GetBlockEntity(x,y,z);
	if(entity) then
		if(entity.GetModelFile) then
			local filename = entity:GetModelFile();
			if(filename) then
				local itemStack = ItemStack:new():Init(self.id, 1);
				-- transfer filename from entity to item stack. 
				itemStack:SetTooltip(filename);
				return itemStack;
			end
		end
	end
end

-- return true if items are the same. 
-- @param left, right: type of ItemStack or nil. 
function ItemBlockModel:CompareItems(left, right)
	if(self._super.CompareItems(self, left, right)) then
		if(left and right and left:GetTooltip() == right:GetTooltip()) then
			return true;
		end
	end
end

function ItemBlockModel:OpenChangeFileDialog(itemStack)
	if(itemStack) then
		local local_filename = itemStack:GetDataField("tooltip");
		NPL.load("(gl)script/apps/Aries/Creator/Game/GUI/OpenFileDialog.lua");
		local OpenFileDialog = commonlib.gettable("MyCompany.Aries.Game.GUI.OpenFileDialog");
		OpenFileDialog.ShowPage(L"请输入bmax, x或fbx文件的相对路径, <br/>你也可以随时将外部文件拖入窗口中", function(result)
			if(result and result~="" and result~=local_filename) then
				itemStack:SetDataField("tooltip", result);
			end
		end, local_filename, L"选择模型文件", "model")
	end
end

-- called whenever this item is clicked on the user interface when it is holding in hand of a given player (current player). 
function ItemBlockModel:OnClickInHand(itemStack, entityPlayer)
	-- if there is selected blocks, we will replace selection with current block in hand. 
	if(GameLogic.GameMode:IsEditor() and entityPlayer == EntityManager.GetPlayer()) then
		local selected_blocks = Game.SelectionManager:GetSelectedBlocks();
		if(selected_blocks and itemStack) then
			-- Save template:
			local last_filename = itemStack:GetDataField("tooltip");
			NPL.load("(gl)script/apps/Aries/Creator/Game/GUI/OpenFileDialog.lua");
			local OpenFileDialog = commonlib.gettable("MyCompany.Aries.Game.GUI.OpenFileDialog");
			OpenFileDialog.ShowPage(L"将当前选择的方块保存为bmax文件. 请输入文件名:<br/> 例如: test", function(result)
				if(result and result~="") then
					local filename = result;
					local bSucceed, filename = GameLogic.RunCommand("/savemodel "..filename);
					if(filename) then
						itemStack:SetDataField("tooltip", filename);
					end
				end
			end, last_filename, L"选择模型文件", "model");
		else
			self:OpenChangeFileDialog(itemStack);
		end
	end
end

-- virtual function: when selected in right hand
function ItemBlockModel:OnSelect()
	GameLogic.SetStatus(L"Ctrl+左键选择方块与骨骼, 左键点击物品图标保存模型");
end

function ItemBlockModel:OnDeSelect()
	GameLogic.SetStatus(nil);
end